﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using IronOne.SalesOne.BusinessObjects;
using System.Configuration;

namespace IronOne.SalesOne.DataObjects
{
   public class LinqCompaintSubType
    {
        public int pageSize = Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]);
       public List<CompaintSubType> GetCompaintSubTypes(string sortExpression ,int page)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                IQueryable<CompaintSubTypeEntity> query = db.CompaintSubTypeEntities;

                if (sortExpression.Length > 0)
                {
                    string[] sort = sortExpression.Split(' ');
                    string sortColumn = sort[0];
                    string sortOrder = sort[1];

                    switch (sortColumn)
                    {
                        case "CodeId":
                            if (sortOrder == "ASC")
                                query = query.OrderBy(c => c.Code_Id);
                            else
                                query = query.OrderByDescending(c => c.Code_Id);
                            break;

                        //case "BranchName":
                        //    if (sortOrder == "ASC")
                        //        query = query.OrderBy(c => c.BranchName);
                        //    else
                        //        query = query.OrderByDescending(c => c.BranchName);
                        //    break;
                    }
                }
                
                var pageData = query.Skip((page - 1) * pageSize).Take(pageSize);
                return pageData.Select(c => Mapper.ToCodeObject(c)).ToList();
            }
        }

       public List<CompaintSubType> GetCompaintSubTypes(string sortExpression)
       {
           using (SODataDataContext db = DataContextFactory.CreateContext())
           {
               IQueryable<CompaintSubTypeEntity> query = db.CompaintSubTypeEntities;

               if (sortExpression.Length > 0)
               {
                   string[] sort = sortExpression.Split(' ');
                   string sortColumn = sort[0];
                   string sortOrder = sort[1];

                   switch (sortColumn)
                   {
                       case "CodeId":
                           if (sortOrder == "ASC")
                               query = query.OrderBy(c => c.Code_Id);
                           else
                               query = query.OrderByDescending(c => c.Code_Id);
                           break;

                       //case "BranchName":
                       //    if (sortOrder == "ASC")
                       //        query = query.OrderBy(c => c.BranchName);
                       //    else
                       //        query = query.OrderByDescending(c => c.BranchName);
                       //    break;
                   }
               }

               //var pageData = query.Skip((page - 1) * pageSize).Take(pageSize);
               return query.Select(c => Mapper.ToCodeObject(c)).ToList();
           }
       }

       public CompaintSubType GetCompaintSubType(int codeId)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                return Mapper.ToCodeObject(db.CompaintSubTypeEntities
                            .SingleOrDefault(p => p.Code_Id == codeId));
            }
        }


        public void InsertCode(CompaintSubType code)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                try
                {

                    CompaintSubTypeEntity entity = Mapper.ToCodeEntity(code);
                    db.CompaintSubTypeEntities.InsertOnSubmit(entity);
                    db.SubmitChanges();

                    code.CodeId = entity.Code_Id;
                }
                catch (ChangeConflictException)
                {
                    throw new Exception("A change to create code country record was made before your changes.");
                }

                catch (Exception ex)
                {
                    throw new Exception("Unable to Insert code country " + code.Code);
                }
            }
        }

        public int UpdateCode(CompaintSubType code)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                try
                {
                    CompaintSubTypeEntity entity = Mapper.ToCodeEntity(code);
                    db.CompaintSubTypeEntities.Attach(entity, true);
                    db.SubmitChanges();
                    return 1;
                }
                catch (ChangeConflictException)
                {
                    throw new Exception("A change to update code record was made before your changes.");
                }
                catch (Exception)
                {
                    return 0;
                }
            }
        }

        public int DeleteCodeCompaintSubType(CompaintSubType code)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                try
                {
                    CompaintSubTypeEntity entity = Mapper.ToCodeEntity(code);
                    db.CompaintSubTypeEntities.Attach(entity, false);
                    db.CompaintSubTypeEntities.DeleteOnSubmit(entity);
                    db.SubmitChanges();

                    return 1;
                }
                catch (ChangeConflictException)
                {
                    throw new Exception("A change to delete code was made before your changes.");
                }
                catch (Exception)
                {
                    return 0;
                }
            }
        }


        
    }
}
